<template>
    <div>
        <div style="margin-top: 20px;" class="page-holder d-flex align-items-center">
    <div style="margin-top: 100px;" class="container">
        <div class="row align-items-center py-5">
            <div class="col-5 col-lg-7 mx-auto mb-5 mb-lg-0">
                <div class="pr-lg-5"><img src="img/222.gif" alt="" class="img-fluid"></div>
            </div>
            
            <div class="col-lg-5 px-lg-4">
             
                <h2 class="mb-4">欢迎注册！</h2>
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                      <el-form-item label="账号" prop="name">
                            <el-input  v-model="ruleForm.name" />
                          </el-form-item>
                      <el-form-item label="密码" prop="pwd">
                        <el-input type="password" v-model="ruleForm.pwd" />
                      </el-form-item>
                      <el-form-item label="确认密码" prop="pwd2">
                        <el-input type="password" v-model="ruleForm.pwd2" />
                      </el-form-item>
                    <el-form-item>
                    <el-button type="primary" @click="onSubmit()">注册</el-button>
                    <span style="margin-left: 100px;"></span>
                   <el-button @click="resetForm()">去登录</el-button> 
                  </el-form-item>
                </el-form>
            </div>
          
        </div>
    </div>
  </div>
    </div>
</template>

<script>
export default {
    data () {
        var name = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        } else {
          if (this.ruleForm.name !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var pwd = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.pwd !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var pwd2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
        return {
          ruleForm:{
                name:"",
                pwd:"",
                pwd2:''
            },
            rules:{
                name:[
                    {validator:name,trigger:'blur'}
                ],
                pwd:[
                    {validator:pwd,trigger:'blur'}
                ],
                pwd2:[
                    {validator:pwd2,trigger:'blur'}
                ],
              }
        }
    },
    methods: {
        onSubmit() {
         this.axios.post("/tomcat/inuser",{username:this.ruleForm.name,userpwd:this.ruleForm.pwd2}).then(data=>{
          if(data.data>0){
              this.$notify({
                  title: '成功',
                  message: '注册成功！',
                  type: 'success',
                  position:'bottom-left'
                });
                setTimeout(()=>{
                  this.$router.push("/login")
                },2000)
            }else{
              this.$notify.error({
                title: '错误',
                message: '注册失败！',
                position:'bottom-left'
              });
            }
         })
        },
        resetForm(){
          this.$router.push("/login")
        }
    },
}
</script>

<style>

</style>